/*
 ***********************************************************
 * @Source 	: DBManager.java
 * Date 	: 2004/xx/xx
 * Title 	: 
 * @Description : 
 ***********************************************************
 * Author : �ֺ��� 	Version 1.0
 * ----------------------------------------------
 * Modify List
 * ----------------------------------------------
 * 2004.10.xx	ȫ�浿			v�� ��d�մϴ�.
 * 2004.11.03	Lim Chae Soo	public static String singleValueResult(ResultSet rs) throws SQLException
 * 2004.12.20	Lim Chae Soo	public static String htmlrplc(String str)
 * 
 * Copyright 2004 Samsung SDS Co., Ltd. All rights reserved.
 ***********************************************************
 */ 


package com.samsungmobile.common.db;

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.samsungmobile.common.db.value.*;

public class DBManager {

	/**
	 * Connection; ����.
	 */
	public static Connection getConnection() {
		InitialContext ic;
		DataSource ds;
		Connection conn=null;

		try {
			ic=new InitialContext();
			ds=(DataSource)ic.lookup("jdbc/oraclePool");
			conn=ds.getConnection();
		} catch (SQLException se) {
			se.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return conn;
	}

	/**
	 * Connection; �ǵ�����.
	 */
	public static void release(Connection conn) {
		try{
			if(conn != null) conn.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	public static void release(Statement stmt,Connection conn) {
		try{
			if(stmt != null) stmt.close();
			release(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	public static void release(PreparedStatement pstmt,Connection conn) {
		try{
			if(pstmt != null) pstmt.close();
			release(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	public static void release(ResultSet rs,Statement stmt ,Connection conn) {
		try{
			if(rs != null) rs.close();
			release(stmt,conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	public static void release(ResultSet rs,PreparedStatement pstmt,Connection conn) {
		try{
			if(rs != null) rs.close();
			release(pstmt,conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

    public static void setValues(PreparedStatement pstmt,Vector values) throws Exception,SQLException,UnsupportedTypeException {

		for(int i=0 ; i<values.size() ; i++) {
            Value v=(Value)values.elementAt(i);
            if (v instanceof BigDecimalValue) {
                pstmt.setBigDecimal(i+1,v.getBigDecimal());
            } else if (v instanceof BooleanValue) {
                pstmt.setBoolean(i+1,v.getBoolean());
            } else if (v instanceof ByteValue) {
                pstmt.setByte(i+1,v.getByte());
            } else if (v instanceof BytesValue) {
                pstmt.setBytes(i+1,v.getBytes());
			} else if (v instanceof DateValue) {
                pstmt.setDate(i+1,v.getDate());
            } else if (v instanceof DoubleValue) {
                pstmt.setDouble(i+1,v.getDouble());
            } else if (v instanceof FloatValue) {
                pstmt.setFloat(i+1,v.getFloat());
			} else if (v instanceof IntValue) {
                pstmt.setInt(i+1,v.getInt());
            } else if (v instanceof LongValue) {
                pstmt.setLong(i+1,v.getLong());
            } else if (v instanceof ShortValue) {
                pstmt.setShort(i+1,v.getShort());
            } else if (v instanceof StringValue) {
                pstmt.setString(i+1,v.getString());
            } else if (v instanceof TimeValue) {
                pstmt.setTime(i+1,v.getTime());
            } else if (v instanceof TimestampValue) {
                pstmt.setTimestamp(i+1,v.getTimestamp());
            } else {
                pstmt.setObject(i+1,v.getObject());
			}
        }

    }


	/*
	 * Query�� ���� ��n�� ���ߵ����� ó�� 
	 */
	public static Vector multiRowResult(ResultSet rs) throws SQLException {
		Vector v=new Vector();
		ResultSetMetaData rsmd=null;
		Properties p=null;
		int cnt=0;

		while (rs.next()) {
			if (cnt == 0) {
				rsmd=rs.getMetaData();
				cnt=rsmd.getColumnCount();
			}

			p=new Properties();

			for (int i=1 ; i<=cnt ; i++) {
				p.setProperty(rsmd.getColumnName(i).toLowerCase(),nullConv(rs.getString(i)));
			}

			v.add(p);
		}

		return v;
	}

	/*
	 * Query�� ���� ��n�� ���ϵ����� ó�� 
	 */
	public static Properties oneRowResult(ResultSet rs) throws SQLException {
		ResultSetMetaData rsmd=null;
		Properties p=null;
		int cnt=0;

		if (rs.next()) {
			p=new Properties();
			rsmd=rs.getMetaData();
			cnt=rsmd.getColumnCount();

			for (int i=1 ; i<=cnt ; i++) {
				p.setProperty(rsmd.getColumnName(i).toLowerCase(),nullConv(rs.getString(i)));
			}
		}

		return p;
	}
	

	/**
	 * ���ϰ� ����
	 * 
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	public static String singleValueResult(ResultSet rs) throws SQLException {
		String reValue = "";
		if (rs.next()) {
			reValue = nullConv(rs.getString(1));
		}

		return reValue;
	}
	
	/**
	*	null �� String; "" �� ����.<br>
	*	trim() �� ��.
	*/
	public static String nullConv(String s) {
		if (s == null) {
			return "";
		} else {
			return s.trim();
		}
	}
	/**
	*	null �� String; 08�� ����.
	*/
	public static String nullConvI(String s) {
		if (s == null) {
			return "0";
		} else {
			return s.trim();
		}
	}
	/**
	*	null �� String; 0.08�� ����.
	*/
	public static String nullConvF(String s) {
		if (s == null) {
			return "0.0";
		} else {
			return s.trim();
		}
	}

}

